package com.guoxue.action.front;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.domain.AlbumVo;
import com.guoxue.domain.ExpertReadingVo;
import com.guoxue.domain.FeedBackVo;
import com.guoxue.domain.ReportVo;
import com.guoxue.domain.TapesRecordsVo;
import com.guoxue.domain.UserVo;
import com.guoxue.service.IAlbumService;
import com.guoxue.service.IExpertReadingService;
import com.guoxue.service.IFeedBackService;
import com.guoxue.service.IReportService;
import com.guoxue.service.ITapesRecordsService;
import com.guoxue.service.IUserService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OSSUtil;
import com.guoxue.util.OtherUtils;
@Controller
@RequestMapping("User")
/**
 * 用户互动交互操作接口处理，主要内容包括：
 * 1.getUserinfoForShare分享页面数据获取
 * 2.tapesReport举报用户及作品
 * 3.feedBack意见反馈
 * 4.tapesTop置顶作品
 * @author kanghongbo
 */
public class UserInterAction extends BaseAction{
	@Resource
	private IUserService userService;
	@Resource
	private OSSUtil oss;
	@Resource
	private ITapesRecordsService tapesService;
	@Resource
	private IAlbumService albumService;
	@Resource
	private IReportService reportService;
	@Resource
	private IFeedBackService feedBackService;
	@Resource
	private IExpertReadingService expertServcie;
	/**
	 * 分享页面数据获取
	 * @param req
	 * @param resp
	 * @param uid
	 * @param tapesId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sharePage")
	public JSON getUserinfoForShare(HttpServletRequest req, HttpServletResponse resp, Integer uid, Integer tapesId, Integer type){
		if (type!=null&&type==1){
			ExpertReadingVo er = expertServcie.getExpertReading(tapesId.toString());
			JSONObject obj = new JSONObject();
			obj.put("url", oss.getUrl(bucket, er.getBookName()+".mp3", new Date(new Date().getTime() + 3600 * 1000 * 24)));
			obj.put("nickName", er.getWorkReader());
			if (er.getPhoto1()!=null){
				obj.put("headUrl", oss.getUrl(bucket, er.getPhoto1(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}else {
				obj.put("headUrl",null);
			}
			JSONArray array = new JSONArray();
			array.add(oss.getUrl(bucket, er.getPhoto1(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			if (!isEmpty(er.getPhoto2())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto3())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto4())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto5())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto6())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto7())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto8())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto9())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (!isEmpty(er.getPhoto10())){
				array.add(oss.getUrl(bucket, er.getPhoto2(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			if (array.size()==0){
				obj.put("imglist", null);
			}else{
				obj.put("imglist", array);
			}
			obj.put("userSignature", er.getIntroduced());
			obj.put("sex", "男");
			obj.put("tapesName", er.getBookName());
			return Message(SUCCESS, "获取分享信息成功", obj);
		}
		if (uid==null){
			return Message(ERROR, "未获取到用户id", null);
		}
		if (tapesId==null){
			return Message(ERROR, "未获取到音频id", null);
		}
		UserVo user = userService.getUserByKey(uid);
		if (user==null){
			return Message(ERROR, "当前用户不存在", null);
		}
		TapesRecordsVo tapes = tapesService.getTapesById(tapesId);
		if (tapes==null){
			return Message(ERROR, "当前音频文件已删除", null);
		}
		JSONObject obj = new JSONObject();
		obj.put("url", oss.getUrl(bucket, tapes.getTapes(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
		obj.put("nickName", user.getNickName());
		if (user.getImgUrl()!=null){
			obj.put("headUrl", oss.getUrl(bucket, user.getImgUrl(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
		}else {
			obj.put("headUrl",null);
		}
		List<AlbumVo> list = albumService.getPhotosList(uid);
		if (list.size()>0){
			JSONArray array = new JSONArray();
			for (AlbumVo a:list){
				array.add(oss.getUrl(bucket, a.getOssKey(), new Date(new Date().getTime() + 3600 * 1000 * 24)));
			}
			obj.put("imglist", array);
		}else {
			obj.put("imglist", null);
		}
		obj.put("userSignature", user.getPersonSignature());
		obj.put("sex", user.getSex()==1?"男":"女");
		obj.put("tapesName", tapes.getChapter());
		return Message(SUCCESS, "获取分享信息成功", obj);
	}
	/**
	 * 作品举报和举报用户
	 * @param req
	 * @param resp
	 * @param uId
	 * 			用户id
	 * @param tapesId
	 * 			音频id
	 * @param content
	 * 			内容
	 * @param reportUId
	 * 			举报者id
	 * @param typeId
	 * 			类型
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/tapesReport")
	public JSON tapesReport(HttpServletRequest req, HttpServletResponse resp,
			Integer uId,Integer tapesId,String content,Integer reportUId,Integer typeId){
		try{
			if(uId==null){
				return Message(ERROR, "未提交uId", null);
			}
			if(typeId.equals(1)){
				if(tapesId==null){
					return Message(ERROR, "未提交tapesId", null);
				}
				if(content==null){
					return Message(ERROR, "未提交content", null);
				}
			}
			if(reportUId==null){
				return Message(ERROR, "未提交reportUId", null);
			}
			ReportVo  reportVo = new ReportVo();
			if(typeId.equals(1)){
				reportVo.setContent(content);
				reportVo.setTapesId(tapesId.toString());
				reportVo.setTypeId(typeId);
			}else{
				reportVo.setTapesId(null);
				reportVo.setTypeId(2);
			}
			reportVo.setReportUId(reportUId.toString());
			reportVo.setuId(uId);
			reportVo.setReportTime(OtherUtils.getCurrentDate());
			reportService.saveReport(reportVo);
			return Message(SUCCESS, "举报成功", null);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "举报失败");
		}
	}
	/**
	 * 意见反馈
	 * @param req
	 * @param resp
	 * @param uId
	 * @param content
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/feedBack")
	public JSON feedBack(HttpServletRequest req, HttpServletResponse resp, Integer uId,String content){
		try{
			if(uId==null){
				return Message(ERROR, "未提交uId", null);
			}
			if(content==null){
				return Message(ERROR, "未提交content", null);
			}
			FeedBackVo  feedBackVo = new FeedBackVo();
			feedBackVo.setContent(content);
			feedBackVo.setuId(uId);
			feedBackVo.setReportTime(OtherUtils.getCurrentDate());
			feedBackService.saveFeedBack(feedBackVo);
			return Message(SUCCESS, "意见反馈成功", null);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "意见反馈失败");
		}
	}
	/**
	 * 置顶
	 * @param req
	 * @param resp
	 * @param uid 用户id
	 * @param tapesId 作品id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="tapesTop")
	public JSON tapesTop(HttpServletRequest req, HttpServletResponse resp,Integer uid,Integer tapesId){
		try{
			if(null == uid){
				return Message(ERROR, "未提交用户id", null);
			}
			if(null == tapesId){
				return Message(ERROR, "未提交作品id", null);
			}
			TapesRecordsVo tapesRecordsVo = tapesService.getTapesById(tapesId);
			if(null == tapesRecordsVo){
				return Message(ERROR, "未上传该作品", null);
			}
			List<TapesRecordsVo> TapesRecordsList = tapesService.findByUidAndStatus(uid, 0);
			for(TapesRecordsVo t :TapesRecordsList){
				if(null != t.getTop()){
					t.setTop(null);
					tapesService.saveTapesRecords(t);
				}
			}
			TapesRecordsVo tapesRecords = tapesService.getTapesById(tapesId);
			tapesRecords.setTop("1");
			tapesService.saveTapesRecords(tapesRecords);
			return Message(SUCCESS, "置顶作品成功", null);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "置顶作品失败");
		}
	}
}
